package com.yzk.auction.dao;

import com.yzk.auction.entity.GoodsDTO;
import com.yzk.auction.entity.ShowOrder;
import com.yzk.auction.entity.Users;
import com.yzk.auction.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * Author: 园中葵小组成员
 * Description:用户相关数据交互，不涉及逻辑代码
 */
public class UserDao {
    private DataSource dataSource = JDBCUtils.getDataSource();
    private Users users = null;

    /**
     * 根据电话号和密码查找用户信息
     * @param tel 电话
     * @param pwd 密码
     * @return User对象
     */
    public Users findByTelPwd(String tel,String pwd){
        QueryRunner qr = new QueryRunner(dataSource);
        try {
            users = qr.query("select * from tb_users where user_tel=? and user_pwd=?",
                    new BeanHandler<>(Users.class), new Object[]{tel,pwd});
            System.out.println(users);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return users;
    }

    /**
     * 根据手机号查询用户
     * @param telephone
     * @return
     */
    public Users findByTel(String telephone){
        QueryRunner qr = new QueryRunner(dataSource);
        try {
            users = qr.query("select * from tb_users where user_tel=? ",
                    new BeanHandler<>(Users.class), new Object[]{telephone});
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return users;
    }

    /**
     * 保存用户信息（注册）
     * @param users
     */
    public void save(Users users){
        QueryRunner qr = new QueryRunner(dataSource);
        try {
            String sql = "INSERT INTO tb_users(user_name, user_pwd, user_sex, user_tel) VALUES (?,?,?,?)";
            qr.update(sql,new Object[]{users.getUser_name(),users.getUser_pwd(),users.getUser_sex(),users.getUser_tel()});
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    public Users findById(Integer id){
        QueryRunner qr = new QueryRunner(dataSource);
        String sql = "select * from tb_users where user_id=?";
        Users users = null;
        try {
            users = qr.query(sql,new BeanHandler<>(Users.class),new Object[]{id});
            System.out.println(users);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return users;
    }

    /**
     * 修改用户信息
     * @param users
     */
    public Integer update(Users users){
        QueryRunner qr = new QueryRunner(dataSource);
        int num = 0;
        String sql = "update tb_users set user_name=?,user_sex=?,user_tel=?,user_addr=?,user_head=? where user_id=?";
        try {
            num = qr.update(sql,new Object[]{users.getUser_name(),users.getUser_sex(),users.getUser_tel(),users.getUser_addr(),users.getUser_head(),users.getUser_id()});
            System.out.println(num);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    /**
     * 根据用户查询所有订单
     * @param id
     * @return
     */
    public List<ShowOrder> findOrderById(Integer id){
        List<ShowOrder> list = null;
        QueryRunner qr = new QueryRunner(dataSource);
        String sql = "SELECT order_id,o.goods_id as goods_id,goods_name,order_time,bidding_price FROM tb_goods g,tb_orders o,tb_bidding b WHERE g.goods_id = o.goods_id and o.user_id=? and b.user_id=? and b.goods_id=o.goods_id";
        try {
            list = qr.query(sql,new BeanListHandler<>(ShowOrder.class),new Object[]{id,id});
            for (ShowOrder s:list){
                System.out.println(s);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
}
